package com.paradigmcreatives.game.ui;

import java.util.Timer;
import java.util.TimerTask;

import com.paradigmcreatives.game.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class CustomTimer extends View {

	
	private static final  String TAG = "CustomTimer";
	Timer timer = null;
	
	Paint greenPaint;
	Paint yellowPaint;
	Paint redPaint;
	
	long totalTime;
	int totalWidth;
	int totalHeight;
	
	float speedInterval = 0;

	float currentWidth;
	
	TimerTask timerTask;
	Handler handler;
	
	int alpha = 0;
	boolean incrdec = true;
	
	public CustomTimer(final Activity context,long time,int width,int height) {
		super(context);
		
		totalWidth = width;
		totalTime = time;
		totalHeight = height;
		
		currentWidth = totalWidth;
		
		speedInterval = (float)totalWidth/(totalTime/100);
		
		timer = new Timer();
		
	
		timerTask = new TimerTask() {
									
			@Override
			public void run() {
				System.out.println("Current width =  "+ currentWidth);
				currentWidth -= speedInterval;
				
				context.runOnUiThread(new Runnable() {
				
				

				@Override
				public void run() {
					
					invalidate();
					/*if(currentWidth < (totalWidth * .33) && currentWidth > (totalWidth * .20)){
						
						Log.i(TAG, "Animation started");
						AlphaAnimation alphaRedAnimation = new AlphaAnimation(255, 30);
						alphaRedAnimation.setDuration(1000);
						alphaRedAnimation.setRepeatMode(Animation.REVERSE);
						alphaRedAnimation.setRepeatCount(Animation.INFINITE);
						CustomTimer.this.startAnimation(alphaRedAnimation);
					}*/
				}
			});
				
			if(currentWidth <= 0){
				this.cancel();
				handler.sendEmptyMessage(101);
//				CustomTimer.this.clearAnimation();
			}
			
			}
		};
		
		
		
		greenPaint = new Paint();
		greenPaint.setAntiAlias(true);
		greenPaint.setColor(Color.GREEN);
		greenPaint.setStrokeCap(Cap.ROUND);
		greenPaint.setStrokeWidth(20);
		
		yellowPaint = new Paint();
		yellowPaint.setAntiAlias(true);
		yellowPaint.setColor(Color.YELLOW);
		yellowPaint.setStrokeCap(Cap.ROUND);
		yellowPaint.setStrokeWidth(20);
		
		
		redPaint = new Paint();
		redPaint.setAntiAlias(true);
		redPaint.setColor(Color.RED);
		redPaint.setStrokeCap(Cap.ROUND);
		redPaint.setStrokeWidth(20);
		
	}

	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(currentWidth > (totalWidth * 0.66) )
			canvas.drawRect(0, 0, currentWidth, totalHeight, greenPaint);
		else if(currentWidth > (totalWidth * 0.33))
			canvas.drawRect(0, 0, currentWidth, totalHeight, yellowPaint);
		else 
			canvas.drawRect(0, 0, currentWidth, totalHeight, redPaint);

	}
	
	
	public void startDisplayingProgress(Handler handler){
		this.handler = handler;
		timer.schedule(timerTask, 100,100);
	}
		
	
	private class AlphaRedAnimation extends AlphaAnimation
	{

		public AlphaRedAnimation(float fromAlpha, float toAlpha) {
			super(fromAlpha, toAlpha);
			
		}
		
	}
	

}
